package at.fhj.swd10.eshop.converter;

import at.fhj.swd10.eshop.domain.Label;
import at.fhj.swd10.eshop.web.application.ShopData;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.apache.commons.lang.StringUtils;

/**
 * @author David
 */
@FacesConverter("at.fhj.swd10.eshop.converter.labelConverter")
public class LabelConverter implements Converter {

    private ShopData shopData;

    public LabelConverter() throws Exception {
        shopData = new ShopData();
    }

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        List<Label> labelList = shopData.getLabelList();
        for (Label label : labelList) {
            if (StringUtils.equals(label.getName(), string)) {
                return label;
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        Label label = (Label) o;
        return label.getName();
    }
}
